package com.heima.xxl.web;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class HelloJob {

    //执行定时任务的方法
    @XxlJob(value = "job-handle-140")//value表示执行的具体的任务名称，任务名称要和admin任务管理中心中的JobHandle名称一致
    public void helloTextJob(){
        /*System.out.println("定时任务执行了。。。"+ LocalTime.now());*/

        //分片的参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        System.out.println("分片数："+shardTotal+",分片序号："+shardIndex);

        //业务逻辑
        List<Integer> list = getList();
        for (Integer integer : list) {
            if(integer % shardTotal == shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行了，任务项为："+integer);
            }
        }
    }

    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
